// pages/goods/detail/index.js
import { reqGoodsInfo } from "@/api/goods"
import { reqAddCart } from "@/api/cart"
// import { userBehavior } from "@/Behaviors/userBehavior"
Page({
  // behaviors:[userBehavior],
  // 页面的初始数据
  data: {
    goodsInfo: {}, // 商品详情
    show: false, // 控制加入购物车和立即购买弹框的显示
    count: 1, // 商品购买数量，默认是 1
    blessing: '', // 祝福语
    buyNow: 0
  },

  // 加入购物车
  handleAddcart() {
    this.setData({
      show: true,
      buyNow: 0
    })
  },
  async handlerSubmit(){
    console.log(1);
    const {token,count,blessing,buyNow} = this.data
    const goodsId = this.goodsId
    if(buyNow === 0){
      const res = await reqAddCart({goodsId,count,blessing})
      if(res.code === 200) {
        wx.toast({title:'加入购物车成功'})
        this.setData({
          show:false
        })
      }
    }
  },
  // 立即购买
  handeGotoBuy() {
    this.setData({
      show: true,
      buyNow: 1
    })
  },

  // 点击关闭弹框时触发的回调
  onClose() {
    this.setData({ show: false })
  },
  previewImage() {
    wx.previewImage({
      urls: this.data.goodsInfo.detailList
    })
  },
  // 监听是否更改了购买数量
  onChangeGoodsCount(event) {
    // console.log(event.detail)
    this.setData({
      count:Number(event.detail)
    })
  },
  async getGoodsInfo() {
    const { data } = await reqGoodsInfo(this.goodsId)
    // console.log(data);
    this.setData({
      goodsInfo: data
    })
  },
  onLoad(options) {
    this.goodsId = options.goodsId
    this.getGoodsInfo()
  }
})
